﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Linq;
using YaAppoint.Devices;
using YaAppoint.Response;

namespace YaAppoint.DeviceTypes
{
    /// <summary>
    /// 设备类型服务
    /// </summary>
    [RemoteService(isEnabled: false)]
    public class DeviceTypeAppService(IRepository<DeviceType, Guid> deviceTypeRepository,
                                      IMapper mapper,
                                      DeviceTypeManager deviceTypeManager,
                                      AsyncQueryableExecuter queryableExecuter) : ApplicationService, IDeviceTypeAppService, ITransientDependency
    {
        private readonly IRepository<DeviceType, Guid> _deviceTypeRepository = deviceTypeRepository;
        private readonly IMapper _mapper = mapper;
        private readonly AsyncQueryableExecuter _queryExecuter = queryableExecuter;
        private readonly DeviceTypeManager _deviceTypeManager = deviceTypeManager;

        /// <summary>
        /// 设备类型列表
        /// </summary>
        /// <returns></returns>
        public async Task<BackJson> GetListAsync(string? filterName)
        {
            var query = await _deviceTypeRepository.GetQueryableAsync();
            if (!string.IsNullOrEmpty(filterName))
            {
                query = query.Where(x=>x.Name.Contains(filterName));
            }
            var list = await _queryExecuter.ToListAsync(query);

            return new BackJson()
            {
                code = 0,
                data = list.Select(x => new
                {
                    id = x.Id,
                    name = x.Name,
                    code = x.Code,
                    outerCode = x.OuterCode,
                    isActive = x.IsActive,
                    desc = x.Desc,
                    modality = x.Modality
                })
            };
        }


        /// <summary>
        ///  获取特定设备类型
        /// </summary>
        public async Task<BackJson> GetAsync(Guid id)
        {
            return new BackJson()
            {
                code = 0,
                data = _mapper.Map<DeviceType, DeviceTypeDto>(await _deviceTypeRepository.GetAsync(id))
            };
        }

        /// <summary>
        ///  删除设备类型
        /// </summary>
        public async Task<BackJson> DeleteAsync(List<Guid> idList)
        {
            foreach (var id in idList)
            { 
                await _deviceTypeManager.DeleteAsync(id);
            }

            return new BackJson()
            {
                code = 0,
                msg = "删除成功"
            };
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        public async Task<BackJson> EditSubmit(DeviceTypeDto dto)
        {
            var deviceType = await _deviceTypeRepository.FirstOrDefaultAsync(x => x.Id == dto.Id);
            if (deviceType == null)
            {
                throw new BusinessException(CustomErrorCodes.DeviceTypeNotExist);
            }

            deviceType.Name = dto.Name;
            deviceType.Code = dto.Code;
            deviceType.OuterCode = dto.OuterCode;
            deviceType.IsActive = dto.IsActive;
            deviceType.Order = dto.Order;

            await _deviceTypeRepository.UpdateAsync(deviceType);

            return new BackJson()
            {
                code = 0,
                msg = "保存成功。"
            };
        }

        /// <summary>
        /// 保存新建
        /// </summary>
        public async Task<BackJson> CreateSubmit(CreateDeviceTypeDto dto)
        {
            var deviceType = _mapper.Map<CreateDeviceTypeDto, DeviceType>(dto);
            var list = await _deviceTypeRepository.GetListAsync(x => x.OuterCode.Equals(dto.OuterCode));
            if (list.Count > 0)
            {
                throw new BusinessException(CustomErrorCodes.OuterCodeAlreadyExist)
                {
                    Data =
                    {
                        { "outerCode",dto.OuterCode}
                    }
                };
            }
            await _deviceTypeRepository.InsertAsync(deviceType);
            return new BackJson()
            {
                code = 0,
                msg = "保存成功"
            };
        } 

    }
}
